import FormPcApi from "@/common/api/form/FormPcApi.js";

/**
 * 处理外部URL表单参数.
 * @author ray
 * @date 2025-02-10
 */
export default  {
    data() {
        return {
            amount: 0
        }
    },
    methods: {
        /**
         * 替换外部URL表单参数.
         * @param urlParams     参数定义
         * @param contextData   上下文参数
         */
        replaceUrlParmaVal(urlParams,contextData) {
            var urlParamData = JSON.parse(urlParams);
            //paramType,paramKey,paramKeyVal
            let urlParamObj={};
            for (let i = 0; i < urlParamData.length; i++) {
                let param = urlParamData[i];
                let paramKey = param.paramKey;
                if (param.paramType) {
                    if(contextData[param.paramKeyVal]){
                        let val = contextData[param.paramKeyVal];
                        urlParamObj[paramKey] = val;
                    }
                }
                else{
                    urlParamObj[paramKey] = param.paramKeyVal;
                }
            }
            return urlParamObj;
        },
        /**
         * 处理外部URL表单参数,使用了防抖处理.
         * @param formId        表单ID
         * @param contextData   上下文参数
         * @returns 返回键值对参数.
         */
        handUrlParams(formId,contextData) {
            var self_ = this;
            if (!formId) {
                return;
            }
            if (this.amount>0) {
                return ;
            }

            FormPcApi.get(formId).then(res => {
                if (!res.success) {
                    self_.$message.error(self_.i18n('URLFail', '获取外部URL表单失败：') + res.message);
                    return;
                }
                let data = res.data;
                var curUser = self_.user;
                contextData.userId =  curUser.userId;
                contextData.fullName = curUser.fullName;
                contextData.account = curUser.account;
                if (data.urlParams) {
                    let urlParamData = self_.replaceUrlParmaVal(data.urlParams,contextData);
                    this.urlParamData = urlParamData;
                }
            });

            this.amount++;
        },
    },


}
